/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.MainFrame;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.menucomponent.PrePlayMatchPanel;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.StorePanel;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.mainmenu.MainMenuPanel;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.filehandlers.IniLoader;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

/**
 * @author Massimo Canonaco
 *
 */
public class GameManagerMenu 
{

	/** */
	static private final String SETTINGS_FILE = "cbvapps"+File.separator+"uiresource"+File.separator+"gamemenuschema.ini";
	
//	/** */
	private JPanel cardPanel;
	
	/** */
	private CardLayout cardLayout;
	
	/** */
	private MainFrame mainFrame;
	
	/** */
	private PrePlayMatchPanel startingMatchPanel;
	
	private StorePanel storePanel;
	
	/** */
	private GameManagerPanel mainPanel;
	Image background;
	/**
	 * @throws IOException 
	 * 
	 */
	public GameManagerMenu(MainFrame frame) throws IOException
	{
		final String BACKGROUND_IMAGE = "gamemenu.background.image";
		IniLoader settingsLoader = new IniLoader(new HashMap<String, String>());
		settingsLoader.load(SETTINGS_FILE);
		final Map settingsMap = settingsLoader.getContents();
		final GameManagerMenu mm = this;
//		final Image background; // = Toolkit.getDefaultToolkit().createImage((String)settingsMap.get(BACKGROUND_IMAGE));
//		background = Toolkit.getDefaultToolkit().createImage((String)settingsMap.get(BACKGROUND_IMAGE));
		mainFrame = frame;
		cardPanel = new JPanel(){
		
//			@Override
//			protected void paintComponent(Graphics g)
//			{
//				super.paintComponent(g);
//				g.fillRect(0, 0,this.getWidth(),this.getHeight());
////				g.drawImage(background, 0, 0,this.getWidth(),this.getHeight(), this);
//			}
		};
		cardPanel.setBackground(Color.black);
//		cardPanel.setMinimumSize(mainFrame.getSize());
//		cardPanel.setSize(mainFrame.getSize());
		cardLayout = new CardLayout();
		cardPanel.setLayout(cardLayout);
		
//		startingMatchPanel = new PrePlayMatchPanel(this, factory);
//		mainPanel = new GameManagerPanel(mm, settingsMap, factory);
//		cardLayout.addLayoutComponent(mainPanel, GameManagerPanel.GAME_MANAGER_PANEL_NAME);
//		cardLayout.addLayoutComponent(startingMatchPanel, PrePlayMatchPanel.PRE_PLAY_MATCH_NAME);
		
//		cardPanel.add(startingMatchPanel);
//		cardPanel.add(mainPanel);
		
	}
	
	public JPanel get()
	{
		return cardPanel;
	}

	/**
	 * @return the mainPanel
	 */
	public GameManagerPanel getMainPanel() 
	{
		return mainPanel;
	}

	/**
	 * @return the mainFrame
	 */
	public MainFrame getMainFrame() {
		return mainFrame;
	}

	/**
	 * @return the cardPanel
	 */
	public JPanel getCardPanel() {
		return cardPanel;
	}

	public void showGM(boolean aBack) {
		
		if(mainPanel == null) {
			IniLoader settingsLoader = new IniLoader(new HashMap<String, String>());
		try {
			settingsLoader.load(SETTINGS_FILE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		final Map settingsMap = settingsLoader.getContents();
		mainPanel = new GameManagerPanel(this, settingsMap);
		cardPanel.add(mainPanel, MainMenuPanel.MAIN_MENU_PANEL_NAME);
		}
		
		cardLayout.show(cardPanel, MainMenuPanel.MAIN_MENU_PANEL_NAME);
		if(startingMatchPanel != null && aBack)
			startingMatchPanel.back();
//		cardPanel = mainPanel;
		
//		cardPanel.removeAll();
//		cardPanel.add(mainPanel);
		mainFrame.switchTo(cardPanel);
	}
	
	public void showSMP() {
		
//		if(startingMatchPanel == null) {
			
			startingMatchPanel = new PrePlayMatchPanel(this);
			cardPanel.add(startingMatchPanel, PrePlayMatchPanel.PRE_PLAY_MATCH_PANEL_NAME);
//		}
//		cardPanel = startingMatchPanel;
		startingMatchPanel.settingCoso();
		cardLayout.show(cardPanel,PrePlayMatchPanel.PRE_PLAY_MATCH_PANEL_NAME);
//		cardPanel.removeAll();
//		cardPanel.add(startingMatchPanel);
//		mainFrame.switchTo(cardPanel);
//		System.out.println("ciao");
	}
	
	public void showStorePanel() {
		if(storePanel == null){
			storePanel = new StorePanel(this);
			cardPanel.add(storePanel, StorePanel.STORE_PANEL_NAME);
		}

		cardLayout.show(cardPanel, StorePanel.STORE_PANEL_NAME);
		if(startingMatchPanel != null)
			startingMatchPanel.back();
//		cardPanel.removeAll();
//		cardPanel.add(storePanel);
//		mainFrame.switchTo(cardPanel);
	}
	
}
